package utils

import (
	"bytes"
	"context"
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
	"log"
)

var (
	AccessKey = "tJbv1tXxnFbujAvZlvYbjMBQ0MeyrylE3QmEo6E0"
	SecretKey = "w4Z8_xW3uM-vZR_gH0ZEAPjidGpPki0wrOi_d1E8"
	BUCKET    = "wzj-file"
	DOMAIN    = "http://file.wzjip.cn"
)

//字节流上传
func QiNiuUploadByte(filename string, data []byte) (string, error) {
	key := filename
	putPolicy := storage.PutPolicy{
		Scope: BUCKET,
	}
	mac := qbox.NewMac(AccessKey, SecretKey)
	upToken := putPolicy.UploadToken(mac)
	cfg := storage.Config{}
	// 空间对应的机房
	cfg.Zone = &storage.ZoneHuanan
	// 是否使用https域名
	cfg.UseHTTPS = false
	// 上传是否使用 CDN上传加速
	cfg.UseCdnDomains = false
	formUploader := storage.NewFormUploader(&cfg)
	ret := storage.PutRet{}
	putExtra := storage.PutExtra{
		Params: map[string]string{
			"x:name": "github logo",
		},
	}
	dataLen := int64(len(data))
	err := formUploader.Put(context.Background(), &ret, upToken, key, bytes.NewReader(data), dataLen, &putExtra)
	if err != nil {
		log.Printf("file: %v", err)
		return "", err
	}
	return ret.Key, nil
}
